package service.impl;

import service.MyString;

import java.util.stream.IntStream;

/**
 * @Author Corday 天涯
 * @Date 2022/3/8 14:17
 */
public class MyStringImpl implements MyString {
    @Override
    public void playString() {
        String str = "abc 123 _ I am a dog!";
        //1.Character,判断字符类型
        int letter = 0, dig = 0, space = 0, el = 0;
        for (int i = 0; i < str.length(); i++) {
            if (Character.isWhitespace(str.charAt(i))) {
                space++;
            } else if (Character.isLetter(str.charAt(i))) {
                letter++;
            } else if (Character.isDigit(str.charAt(i))) {
                dig++;
            } else {
                el++;
            }//if
        }//for
        System.out.println("字母数:" + letter);
        System.out.println("数字数:" + dig);
        System.out.println("空格数:" + space);
        System.out.println("其它:" + el);
        //2.常用的API
        String s = "I am zhangsan!";
        String[] chars = s.split(" ");
        CharSequence cs = s.subSequence(1, 5);
        System.out.println(cs.toString());
        for (int i = 0; i < cs.length(); i++) {
            System.out.print(cs.charAt(i) + " ");
        }
        System.out.println();
        //cs.chars()转换成intStream流，功能更强大
        System.out.println("2-6的子串：" + s.substring(2, 6));
        //字符串串接
        s.concat("不知转入此中来");
        String sdig = "856.78";
        double money = 9800;
        double temp = Double.valueOf(sdig);
        System.out.println(money + temp);
        String vali = "262234526786";
        System.out.println("------subSequence的用法-------");
        IntStream ins = vali.subSequence(1, 10).chars();
        // System.out.println("max:"+ins.max());
        //System.out.println("count:"+ins.count());
//        System.out.println("avg:"+ins.average());
        System.out.println("distinct:" + ins.distinct());
//       关闭流
        ins.close();
    }

    @Override
    public void playStringBuilder() {
//     支持单线程，保存多次字符串的串接结果
        StringBuilder sbd = new StringBuilder("开始");
        for (int i = 100; i > 50; i--) {
            sbd.append(i + " ");
        }
        System.out.println(sbd.toString());
        sbd.deleteCharAt(2);
        sbd.charAt(3);
        sbd.delete(2, 5);
        sbd.toString();
        //设置为空对象
        sbd = null;
    }

    @Override
    public void playStringBuffer() {
//  支持多线程，用于保存字符串，管理字符串
        StringBuffer sbf = new StringBuffer();
        sbf.append(100);
        sbf.append(90.5);
        sbf.append(new int[]{1, 2, 3});
        sbf.append("tom");
        //替换某些位置的字符串
        sbf.replace(2, 4, "abc");
        //替换某个位置的字符
        sbf.setCharAt(5, 'a');
        String temp = sbf.toString();
    }
}
